package com.ChenYaRou.www.utils;

import com.ChenYaRou.www.constant.JsonBeanConstant;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;

/**
 * @ClassName SensitiveFilter
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/2 14:44
 * @Version 1.0
 **/
public class SensitiveFilter {
    private volatile static SensitiveFilter sensitiveFilter;
    private static HashSet<String> sensitiveWords=new HashSet<>();
    private SensitiveFilter(){}
    public static SensitiveFilter getInstance(){
        if(sensitiveFilter==null){
            synchronized (SensitiveFilter.class){
                if(sensitiveFilter==null){
                    sensitiveFilter=new SensitiveFilter();
                }
            }
        }
        return sensitiveFilter;
    }

    static {
        try {
            BufferedReader bufferedReader=new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream("C:\\小A二轮\\MeiTuan\\src\\main\\resources\\SensitiveWords"),
                            StandardCharsets.UTF_8));
            String word;
            while((word=bufferedReader.readLine())!=null){
                sensitiveWords.add(word);
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 检阅敏感词 并返回json告知是否含有敏感词
     * @param words
     * @return
     */
    public String sensitiveFilter(String words){
        if(words!=null){
            for (String sensitiveWord : sensitiveWords) {
                if(words.contains(sensitiveWord)){
                    return JsonBean.operateList(JsonBeanConstant.HAVE_SENSITIVE_WORD,null);
                }
            }
            return null;
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

}
